package com.gin.common.util.mybatis.page;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.ibatis.session.RowBounds;

import java.io.Serializable;

/**
 * create  on 2020-02-26 14:54
 * 后端管理系统：分页
 */
public class Page<T> extends RowBounds implements Serializable {

    private Integer pageIndex;
    private Integer pageSize;
    private Integer total; // 总条目数量
    /**
     * 是否每次都查询：总条目数量；默认 true
     */
    @JsonIgnore
    private boolean allSelect = true;
    /**
     * count sql 的类型
     */
    @JsonIgnore
    private CountModel countModel;
    // 总共多少页
    private int pages;
    // 数据列表
    private T items;

    public Page() {
        this.pageIndex = 0;
        this.pageSize = 20;
        this.countModel = CountModel.SUB_FROM_SQL;
    }

    public Page(Integer pageIndex, Integer pageSize) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
    }

    public Page(Integer pageIndex, Integer pageSize, Integer total) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.total = total;
    }


    public T getItems() {
        return items;
    }

    public void setItems(T items) {
        this.items = items;
    }

    public CountModel getCountModel() {
        return countModel;
    }

    public void setCountModel(CountModel countModel) {
        this.countModel = countModel;
    }

    /**
     * 一共多少页
     *
     * @return
     */
    public int getPages() {
        if (this.total != 0) {
            if (this.total % this.pageSize == 0) {
                return this.total / this.pageSize;
            }
            return (this.total / this.pageSize) + 1;
        }
        return 0;
    }

    /**
     * 是否总数查询count
     *
     * @return
     */
    public boolean isAllSelect() {
        return this.allSelect;
    }

    /**
     * 设置;是否每次都查询：总条目数量
     *
     * @param allSelect
     */
    public void setAllSelect(boolean allSelect) {
        this.allSelect = allSelect;
    }

    /**
     * 返回总条目数量
     *
     * @param total
     */
    public void setTotal(int total) {
        this.total = total;
    }

    /**
     * 获取偏移量
     *
     * @return
     */
    @JsonIgnore
    /**
     * 获取当前页的：开始偏移量
     *
     * @return
     */
    public Integer getOffsetExt() {
        int offset = (pageIndex - 1) * pageSize;
        return offset;
    }

    /**
     * 获取当前页的 最后一个 偏移量
     *
     * @return
     */
    public Integer getEnd() {
        if (null == total) {
            throw new IllegalArgumentException("请传递 total 属性.");
        }
        int end = this.getOffset() + pageSize;
        end = end > total ? total : end;
        return end;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Integer getTotal() {
        return total;
    }

    @Override
    @JsonIgnore
    public int getLimit() {
        return super.getLimit();
    }

    @JsonIgnore
    @Override
    public int getOffset() {
        return super.getOffset();
    }

    public Integer getPageIndex() {
        return pageIndex;
    }
}
